package com.young4j.springdemo.controller;

import com.young4j.springdemo.domain.Student;
import com.young4j.springdemo.domain.TermStage;
import com.young4j.springdemo.mongoRepository.MongoStudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

/**
 * @Description
 * @Author yf
 * @Date 2019/1/2 16:08
 **/
@RestController
@RequestMapping("/mongodb")
public class MongoDbController {
    @Autowired
    private MongoStudentRepository mongoStudentRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    @RequestMapping("/add")
    public Object add(){
        Random random = new Random();
        long id = random.nextLong();
        Student s =  new Student(id, String.valueOf(id));
        mongoTemplate.save(s);
        //Student save = mongoStudentRepository.save(new Student(id, String.valueOf(id)));
        return s;
    }

    @RequestMapping("/list")
    public Object list(){
        List<Student> all = mongoTemplate.findAll(Student.class);
        return all;
    }
    @RequestMapping("/tadd")
    public Object tadd(){
        Random random = new Random();
        long id = random.nextLong();
        TermStage ts = new TermStage(id,String.valueOf(id));
        mongoTemplate.save(ts);
        return ts;
    }

    @RequestMapping("/tlist")
    public Object tlist(){
        List<TermStage> all = mongoTemplate.findAll(TermStage.class);
        return all;
    }



}
